package Network;

import Interfaces.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.net.*;
import java.io.*;

/**
 * Logs changes in the reachabiltiy of an IP - address periodically until stopped
 * 
 * @author Paul Thorwirth
 * @version 1.2
 *
 * @see File
 * @see BufferedWriter
 * @see Socket
 */
public class NetworkLOG implements Globals
{
	private File path;
	private File file;
	private BufferedWriter writer;
	private String logfile;
	private SimpleDateFormat dateformat;
	private SimpleDateFormat timeformat;
	private Date date;

	private boolean reachable;
	private boolean last;
	private Socket socket;
	private InetAddress ip;
	private InetSocketAddress port;

	private Thread checkerThread;
	private boolean stop = false;

	/**
	 * Initializes the Socket and the Writer. A new Tread is defined for the background logging.
	 * 
	 * @see Socket
	 * @see BufferedWriter
	 * @see Thread
	 */
	public NetworkLOG(final String ipStr, final String dir, final int checkInterval) 
	{
		date = new Date();
		dateformat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
		timeformat = new SimpleDateFormat("HH:mm:ss");
		logfile = "PING_LOG_"+dateformat.format(date)+".txt";

		path = new File(dir);
		path.mkdirs();
		file = new File(path, logfile);
		try 
		{
			file.createNewFile();
		} catch (IOException e) {}
		file.canWrite();
		file.canRead();
		try 
		{
			writer = new BufferedWriter(new FileWriter(file));
		} catch (IOException e) { }

		checkerThread = new Thread(new Runnable()
		{
			public void run()
			{
				boolean firstRun = true;
				do
				{
					reachable = true;
					socket = new Socket();
					try 
					{
						ip = InetAddress.getByName(ipStr);
						port = new InetSocketAddress(ip, 80);
						socket.connect(port);
						socket.close();
					}
					catch (UnknownHostException e) { reachable = false; }
					catch (IOException e) { reachable = false; }
					try
					{
						if (firstRun)
						{
							last = !reachable;
							
							if (ip != null) writer.write("IP: " + ipStr +" (" + ip.getHostAddress() + ")");
							else writer.write("IP: " + ipStr + " couldn't be resolved");
							
							writer.newLine();
							writer.write("Output Directory: " + dir);
							writer.newLine();
							writer.write("Checking Interval: " + checkInterval);
							writer.newLine();
							writer.newLine();
							firstRun = false;
						}

						if (reachable == !last)
						{
							date = new Date();
							writer.write(timeformat.format(date) + " ");
							writer.write("IP: " + ipStr + " ");
							
							writer.write((reachable) ? "is reachable" : "is NOT reachable");
						
							writer.newLine();
							writer.flush();
						}
						
						last = reachable;
					} catch (IOException e) { }

					try 
					{
						Thread.sleep(checkInterval * 1000);
					} catch (InterruptedException e) { }
				} while (!stop);

				try
				{
					writer.close();
				} catch (IOException e) { }
			}
		});
	}

	/**
	 * Defined by Globals Interface. Starts the periodic logging.
	 * 
	 * @see Globals
	 */
	public void start()
	{
		checkerThread.start();
	}

	/**
	 * Defined by Globals Interface. Stops the periodic logging.
	 * 
	 * @see Globals
	 */
	public void stop()
	{
		stop = true;
	}
}

